﻿using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Infrastructure.Web.Mvc.Extensions;

namespace Gama.Website
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }
        protected void Session_End()
        {
            // Code that runs when a session ends.
        }


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.Clear();

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
            routes.IgnoreRoute("Content/{*content}");
            routes.IgnoreRoute("Scripts/{*script}");

            routes.MapRoute(null, new Regex(@"resource/(?<action>.+?)/(?<version>.+?)/(?<resourceName>.+)"), new { controller = "Resource" });
            ////////////////////////////////////////////////////
            #region "Home Controler"
            routes.MapRoute(
                "Error404",
                "trang-bi-loi",
                new { controller = "Error", action = "Error404" }
            );

            routes.MapRoute(
                 "Index",
                 new Regex(@"(?<lang>[\w,-]+)/Index"),
                 new { lang="en-US", controller = "Home", action = "Index" }
             );
                         
            #endregion             
            //Default router
            routes.MapRoute("Default", new Regex(@"Index"), new { controller = "Home", action = "Index" });
            //routes.MapRoute("Default", new Regex(@"KinhTe(/index)?"), new { controller = "Home", action = "Show" });

            routes.MapRoute("",
                            "{controller}/{action}/{param}",
                            new { controller = "Home", action = "Index", param = "" });

            routes.MapRoute("Error", new Regex(@"\w"), new { controller = "Error", action = "UnresolvableRequest" });
        }

        // redirects default location
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var context = ((HttpApplication)sender).Context;

            var requestUrl = context.Request.Url.AbsoluteUri;

            if (requestUrl.Contains("://nhasachkinhte.com"))
            {
                requestUrl = requestUrl.Replace("://", "://www.");

                Response.Clear();
                Response.StatusCode = (int)HttpStatusCode.MovedPermanently; ;
                Response.Status = "301 Moved Permanently";
                Response.StatusDescription = "Moved Permanently";
                Response.AddHeader("Location", requestUrl);
                Response.Flush();
                Response.End();
            }

            var relativeFilePath = context.Request.AppRelativeCurrentExecutionFilePath;

            if (relativeFilePath == "~/" ||
                String.Equals(relativeFilePath, "~/default.aspx", StringComparison.InvariantCultureIgnoreCase))
            {
                context.Response.Redirect(string.Format( "~/{0}/Index",Global.Helpers.GamaOptions.LanguageDefault));
            }
        }
    }
}